home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: String assignments, please help (beginner)
- Date: Wed, 10 Apr 96 01:14:28 GMT
- Organization: none
- Message-ID: <829098868snz@genesis.demon.co.uk>
- References: <4ke05g$27q@soap.news.pipex.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4ke05g$27q@soap.news.pipex.net> tone@dial.pipex.com "Tone" writes:
-
- >I've just been learning C for the last few weeks and have come across
- >a problem with assigning strings, which I will detail below.
- >(Sorry the posting is so long, I'm just trying to make sure you know
- >exactly what my problem is):
- >
- >I was originally informed that the following was valid:
- >
- > char string1[30];
- > string1 = "Test string";
-
- No, you can't assign to an array in C.
-
- >This gave me an error on the second line although when declaring I am
- >able to use:
- >
- > char string1[30] = "Test string";
-
- C permits array initialisations.
-
- >I have then read up and found the following info elsewhere (quote):
- >
- > a = "This is a string.";
- > is only possible if a is a char pointer.
-
- Not 100% true but true for nearly all practical purposes.
-
- >so I arrived at the following:
- >
- > char *string1[30];
- > *string1 = "Test string";
-
- Not what you wanted. Here string1 is an array of 30 pointers to char and
- you assigned the first array element to point to your string. You can write:
-
- char *string2;
- string2 = "Test string";
-
-
- >This compiles okay and gives the desired result in the small program I
- >was writing.
-
- It would probably work the way you used it but you are still carrying
- around 29 unused pointers!
-
- >However, I have since found the following information (quote):
- >
- > int *c;
- > *c = 4;
- >
- > can and probably will give some unexpected and unwelcome results!
- > As the value of c is random, the memory-address to which 4 is
- > assigned is random, too. This means that you could change basically
- > any location in the memory, including machine code.
-
- That is true but in the previous cases the assignment was to the pointer
- itself and not what the pointer pointed to so they don't dereference an
- uninitialised pointer.
-
- >I am assuming that these "unexpected results" mentioned above will
- >also apply to the code I have written.
-
- No, in your code *string1 references the first element of the array, it is
- the same as writing string1[0]. The C language definition makes them
- interchangeable - a[b] is defined by the language as (*(a + (b))). So a[0]
- is (*(a + (0)) i.e. *a.
-
- >The only other way I know of assigning a string is to create a loop
- >(e.g. a "for" loop) and assign each character of the string
- >individually and manually add the '\0' at the end. This seems
- >extremely laborious and I'm sure it can't be the best way. (Also it
- >may give similar "unexpected results"?)
-
- By assigning a string I assume you mean copying it from one array to another.
- The strcpy() function is provided to do just this.
-
- #include <string.h>
-
- ...
-
- char string1[30];
-
- strcpy(string1, "Test string");
-
-
- >My questions are as follows:
- >
- >1) When using pointers as shown above, do I need to set the memory
- >location of the string variable in some way before using it (and if
- >so, how).
-
- Pointers need to point to a valid object before you can dereference them.
- It is up to you to make sure that they do. If you haven't explicitly caused
- a pointer to point to something via initialisation or assignment then
- it doesn't point to anything valid.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-